﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;

namespace SZ
{
    class ContextMenu
    {
        private IToolbarMenu m_menuRsLayer = null;
        private IToolbarMenu m_menuLayer = null;
        private IToolbarMenu m_menuMap = null;

        private AxMapControl m_mapControl = null;
        private AxTOCControl m_tocControl = null;
        private MainFrm m_mainFrm = null;
        private TreeView m_treeView = null;


        public ContextMenu(MainFrm mainFrm, AxMapControl mapControl, AxTOCControl tocControl, TreeView treeView)
        {
            m_mainFrm = mainFrm;
            m_tocControl = tocControl;
            m_mapControl = mapControl;
            m_treeView = treeView;

            m_tocControl.OnMouseDown += new ITOCControlEvents_Ax_OnMouseDownEventHandler(m_tocControl_OnMouseDown);
        }

        public void CreateContextMenu()
        {
            m_menuMap = new ToolbarMenuClass();
            m_menuMap.AddItem(new ClearAllLayers(), -1, 0, true, esriCommandStyles.esriCommandStyleTextOnly);

            m_menuRsLayer = new ToolbarMenuClass();
            m_menuRsLayer.AddItem(new ZoomToLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuRsLayer.AddItem(new RemoveLayers(m_treeView), -1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);

            m_menuLayer = new ToolbarMenuClass();
            m_menuLayer.AddItem(new ZoomToLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new RemoveLayers(m_treeView), -1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new OpenAttribute(m_mainFrm), -1, 2, true, esriCommandStyles.esriCommandStyleTextOnly);

            m_menuMap.SetHook(m_mapControl);
            m_menuLayer.SetHook(m_mapControl);
            m_menuRsLayer.SetHook(m_mapControl);
            m_tocControl.SetBuddyControl(m_mapControl);

        }

        private void m_tocControl_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button != 2)
            {
                return;
            }

            esriTOCControlItem pItem = esriTOCControlItem.esriTOCControlItemNone; // 右击的item
            IBasicMap pMap = null;
            ILayer pLayerHit = null;
            object pOther = new object();
            object pIndex = new object();
            m_tocControl.HitTest(e.x, e.y, ref pItem, ref pMap, ref pLayerHit, ref pOther, ref pIndex);

            if (pItem == esriTOCControlItem.esriTOCControlItemMap)
            {
                m_tocControl.SelectItem(pMap, null);
            }
            else if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
            {
                m_tocControl.SelectItem(pLayerHit, null);
            }
            m_mapControl.CustomProperty = pLayerHit;

            // 弹出右键菜单
            if (pItem == esriTOCControlItem.esriTOCControlItemMap)
            {
                m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd);
            }
            else if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
            {
                if (pLayerHit is IRasterLayer)
                {
                    m_menuRsLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
                }
                else
                {
                    m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);
                }
            }


 
        }


        public void ShowAllNodes(TreeNodeCollection nodes, string name)
        {
            foreach (TreeNode treeNode in nodes)
            {
                if (treeNode.Nodes.Count > 0)
                {
                    ShowAllNodes(treeNode.Nodes, name);

                }
                else
                {
                    if (treeNode.Text == name)
                    {
                        treeNode.Checked = false;

                    }

                }
            }
        }
    }
}
